package com.qdlc.p2p.manage.action.additional;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;

import com.opensymphony.xwork2.ModelDriven;
import com.qdlc.p2p.biz.service.AdditionalMovieTicketRuleService;
import com.qdlc.p2p.biz.web.action.BaseAction;
import com.qdlc.p2p.common.exception.MovieTicketException;
import com.qdlc.p2p.common.util.DateUtil;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.dto.AdditionalMovieTicketRule;
import com.qdlc.p2p.dal.dto.Operator;
import com.qdlc.p2p.dal.model.AdditionalMovieTicketRuleModel;

/**
 * Created by liaoxiangkai on 2015/11/27.
 */
public class ManageMovieTicketAction extends BaseAction<AdditionalMovieTicketRuleModel> implements ModelDriven<AdditionalMovieTicketRuleModel> {
    @Resource
    private AdditionalMovieTicketRuleService additionalMovieTicketRuleService;

    private AdditionalMovieTicketRuleModel model = new AdditionalMovieTicketRuleModel();

    public AdditionalMovieTicketRuleModel getModel() {
        return model;
    }

    private Map<String, Object> data;

    /**
     * ------------------后台电影票规则------------------------
     * 进入电影票规则页面
     * @return
     */
    @Action("/modules/additional/movieTicket/movieTicketManager")
    public String movieTicketManager(){
        return "movieTicketManager";
    }

    /**
     * 电影票规则列表
     */
    @Action("/modules/additional/movieTicket/movieTicketList")
    public void movieTicketList() throws IOException {
        data = new HashMap<String, Object>();
        PageDataList<AdditionalMovieTicketRuleModel> pageDataList = additionalMovieTicketRuleService.list(model);
        data.put("total", pageDataList.getPage().getTotal());
        data.put("rows", pageDataList.getList());
        printJson(getStringOfJpaObj(data));
    }

    /**
     * --------------------添加电影票规则--------------------------
     * 进入页面
     *
     * @return
     * @throws Exception
     */
    @Action("/modules/additional/movieTicket/movieTicketAddPage")
    public String movieTicketAddPage() throws Exception {
        return "movieTicketAddPage";
    }

    /**
     * 电影票规则添加动作
     *
     * @throws Exception
     */
    @SuppressWarnings("static-access")
    @Action("/modules/additional/movieTicket/movieTicketAdd")
    public void movieTicketAdd() throws Exception {
        //检测添加电影票规则参数
        checkDrawMovieTicket(model);
        Operator operator = getOperator();
        if (operator != null) {
            model.setCreator(operator);
            model.setCreateTime(DateUtil.getNow());
        }
        AdditionalMovieTicketRule additionalMovieTicketRule = additionalMovieTicketRuleService.save(model);
        if (additionalMovieTicketRule == null) {
            printResult("添加电影票规则失败！", false);
            return;
        }
        printResult("添加电影票规则成功！", true);
    }

    /**
     * -------------------编辑电影票规则-----------------------------
     * 进入页面
     *
     * @return
     * @throws Exception
     */
    @Action("/modules/additional/movieTicket/movieTicketEditPage")
    public String movieTicketEditPage() throws Exception {
        AdditionalMovieTicketRule movieTicketRule = additionalMovieTicketRuleService.find(model.getId());
        if (movieTicketRule == null) {
            printResult("该电影票规则不存在!", false);
            return "movieTicketManager";
        }
        request.setAttribute("movieTicketRule", movieTicketRule);
        return "movieTicketEditPage";
    }

    /**
     * 电影票规则更新动作
     *
     * @throws Exception
     */
    @Action("/modules/additional/movieTicket/doMovieTicketEdit")
    public void domovieTicketEdit() throws Exception {
        //检测添加电影票规则参数
        checkDrawMovieTicket(model);
        //更新电影票规则
        Operator operator = getOperator();
        if (null == operator) {
            printResult("请先登录！", false);
            return;
        }
        model.setModifier(operator);
        model.setModifyTime(DateUtil.getNow());
        AdditionalMovieTicketRule rule = additionalMovieTicketRuleService.update(model);
        if (null == rule) {
            printResult("修改电影票规则失败！", false);
            return;
        }
        printResult("修改电影票规则成功！", true);
    }

    /**
     * ----------------电影票规则详情页面-------------------
     *
     * @return
     * @throws Exception
     */
    @Action("/modules/additional/movieTicket/movieTicketDetailPage")
    public String movieTicketDetailPage() throws Exception {
        AdditionalMovieTicketRule movieTicketRule = additionalMovieTicketRuleService.find(model.getId());
        if (movieTicketRule == null) {
            printResult("该电影票规则不存在!", false);
            return "movieTicketManager";
        }
        request.setAttribute("movieTicketRule", movieTicketRule);
        return "movieTicketDetailPage";
    }

    /**
     * --------------------是否开启本条规则-----------------------
     * 开启规则
     *
     * @throws IOException
     */
    @Action("/modules/additional/movieTicket/movieTicketEnable")
    public void movieTicketEnable() throws IOException {
        Operator operator = getOperator();
        if (null == operator) {
            printResult("请先登录！", false);
            return;
        }
        model.setModifier(operator);
        model.setModifyTime(DateUtil.getNow());
        int result = additionalMovieTicketRuleService.toggleStatus(model);
        if (result > 0) {
            printWebSuccess();
        } else {
            printWebResult("启用失败，请确认活动规则是否已开启！", false);
        }
    }

    /**
     * 关闭规则
     *
     * @throws IOException
     */
    @Action("/modules/additional/movieTicket/movieTicketDisable")
    public void movieTicketDisable() throws IOException {
        Operator operator = getOperator();
        if (null == operator) {
            printResult("请先登录！", false);
            return;
        }
        model.setModifier(operator);
        model.setModifyTime(DateUtil.getNow());
        int result = additionalMovieTicketRuleService.toggleStatus(model);
        if (result > 0) {
            printWebSuccess();
        } else {
            printWebResult("关闭失败，请确认活动规则是否已关闭！", false);
        }
    }


    /**
     * ------------------校验方法-----------------------
     * 检验添加电影票规则参数
     *
     * @param model
     * @return
     */
    private void checkDrawMovieTicket(AdditionalMovieTicketRuleModel model) throws Exception {
        if (null == model) {
            throw new MovieTicketException("设置电影票规则模型为空!");
        }
        if (model.getStatus() != 0 && model.getStatus() != 1) {
            throw new MovieTicketException("设置电影票规则状态不合法!");
        }
        if (model.getCount() <= 0) {
            model.setCount(1);
        }
    }

}
